﻿using System;
using System.IO;
using System.Xml;
using Ro.AssistFunction.BackNodeInfo;
using Ro.AssistFunction.OutputManage;
using Ro.Common.RoArgs;
using Ro.Common.RoInterface;
using Ro.Common.RoInterface.RoWebAction;
using Ro.Common.RoInterface.UserType;
using Ro.DriverEvent.WebAction;

namespace Ro.Interpreter.RunLogic
{
    /// <summary>
    /// 构造类
    /// 针对单个测试步骤进行有效信息的提取工作，
    /// </summary>
    public class RunStepsQuene
    {
        /// <summary>
        /// 反馈该步的执行结果
        /// 不考虑究竟是true,false,null
        /// </summary>
        public bool BackStepResult;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sigteststep">单个测试步骤节点</param>
        public RunStepsQuene(XmlNode sigteststep)
        {
            try
            {
                /* todo 一个步骤有3个必备信息，步骤操作，是否切换警告，超时。后两者运行默认不写
                *  其他特有信息包括 控件Id, 输入值，切换大小写等 各个操作对应的特有信息并不相同，因此不能直接使用反序列化进行
                *  3-17 当前未确定返回类型，暂时不写返回
                *  3-24 完成事件驱动的开发工作，包含三态判断
                *  4-25 回复为true/false判断，不再包含三态判断(因为毫无意义)
                */


                //初始化
                IWebAlertFunc webAlertFunc = new WebAlert();
                IWebBrowserFunc webBrowserFunc = new WebBrowser();
                IWebLaunchFunc webLaunchFunc = new WebLaunch();
                IWebMouseFunc webMouseFunc = new WebMouse();
                IWebRoWebElementFunc webRoWebElementFunc = new WebRoWebElement();
                IWebScrollFunc webScrollFunc = new WebScroll();
                IWebSleepFunc webSleepFunc = new WebSleep();
                IWebUpdateFunc webUpdateFunc = new WebUpdate();
                IWebWaitUntilFunc webWaitUntilFunc = new WebWaitUntil();

                ExtractNeedInfo extractNeedInfo = new ExtractNeedInfo();

                //单个步骤的结果
                WebActionResult back = new WebActionResult();

                //初始化输出
                IRoOutputToConsole roOutputToConsole = new RoOutputToConsole();

                //是否将错误更改为警告
                bool ew = extractNeedInfo.ExtractTreatBool(sigteststep);


                //循环
                switch (sigteststep.Name)
                {
                    case RoWebStep.WebLaunchFirefox:
                        back = webLaunchFunc.Firefox(sigteststep);
                        break;
                    case RoWebStep.WebLaunchChrome:
                        back = webLaunchFunc.Chrome(sigteststep);
                        break;
                    case RoWebStep.WebLaunchInternetExplorer:
                        back = webLaunchFunc.Explorer(sigteststep);
                        break;

                    case RoWebStep.WebAlertAccept:
                        back = webAlertFunc.Accept(sigteststep);
                        break;
                    case RoWebStep.WebAlertDismiss:
                        back = webAlertFunc.Dismiss(sigteststep);
                        break;
                    case RoWebStep.WebAlertSendKeys:
                        back = webAlertFunc.SendKeys(sigteststep);
                        break;

                    case RoWebStep.WebBrowserBack:
                        back = webBrowserFunc.WebBrowserBack(sigteststep);
                        break;
                    case RoWebStep.WebBrowserClose:
                        back = webBrowserFunc.WebBrowserClose(sigteststep);
                        break;
                    case RoWebStep.WebBrowserCloseTab:
                        back = webBrowserFunc.WebBrowserCloseTab(sigteststep);
                        break;
                    case RoWebStep.WebBrowserExecuteScript:
                        back = webBrowserFunc.WebBrowserExecuteScript(sigteststep);
                        break;
                    case RoWebStep.WebBrowserForward:
                        back = webBrowserFunc.WebBrowserForward(sigteststep);
                        break;
                    case RoWebStep.WebBrowserGoToUrl:
                        back = webBrowserFunc.WebBrowserGoToUrl(sigteststep);
                        break;
                    case RoWebStep.WebBrowserRefresh:
                        back = webBrowserFunc.WebBrowserRefresh(sigteststep);
                        break;
                    case RoWebStep.WebBrowserStop:
                        back = webBrowserFunc.WebBrowserStop(sigteststep);
                        break;
                    case RoWebStep.WebBrowserSwitchFrame:
                        back = webBrowserFunc.WebBrowserSwitchFrame(sigteststep);
                        break;
                    case RoWebStep.WebBrowserSwitchToTab:
                        back = webBrowserFunc.WebBrowserSwitchToTab(sigteststep);
                        break;
                    case RoWebStep.WebBrowserTakeSnapshot:
                        back = webBrowserFunc.WebBrowserTakeSnapshot(sigteststep);
                        break;

                    case RoWebStep.WebMouseMove:
                        back = webMouseFunc.MouseMove(sigteststep);
                        break;
                    case RoWebStep.WebMouseClick:
                        back = webMouseFunc.MouseClick(sigteststep);
                        break;

                    case RoWebStep.WebRoWebElementClear:
                        back = webRoWebElementFunc.WebRoWebElementClear(sigteststep);
                        break;
                    case RoWebStep.WebRoWebElementClick:
                        back = webRoWebElementFunc.WebRoWebElementClick(sigteststep);
                        break;
                    case RoWebStep.WebRoWebElementFocus:
                        back = webRoWebElementFunc.WebRoWebElementFocus(sigteststep);
                        break;

                    case RoWebStep.WebRoWebElementSelect:
                        back = webRoWebElementFunc.WebRoWebElementSelect(sigteststep);
                        break;
                    case RoWebStep.WebRoWebElementSendKeys:
                        back = webRoWebElementFunc.WebRoWebElementSendKeys(sigteststep);
                        break;

                    case RoWebStep.WebScrollDown:
                        back = webScrollFunc.Down(sigteststep);
                        break;
                    case RoWebStep.WebScrollUp:
                        back = webScrollFunc.Up(sigteststep);
                        break;

                    case RoWebStep.WebSleep:
                        back = webSleepFunc.Sleep(sigteststep);
                        break;

                    case RoWebStep.WebUpdateSelect:
                        back = webUpdateFunc.UpdateSelect(sigteststep);
                        break;

                    case RoWebStep.WebWaitUntilPageIsLoaded:
                        back = webWaitUntilFunc.WebWaitUntilPageIsLoaded(sigteststep);
                        break;
                    case RoWebStep.WebWaitUntilAreEqual:
                        back = webWaitUntilFunc.WebWaitUntilAreEqual(sigteststep);
                        break;
                    case RoWebStep.WebWaitUntilAreNotEqual:
                        back = webWaitUntilFunc.WebWaitUntilAreNotEqual(sigteststep);
                        break;
                    case RoWebStep.WebWaitUntilIsFalse:
                        back = webWaitUntilFunc.WebWaitUntilIsFalse(sigteststep);
                        break;
                    case RoWebStep.WebWaitUntilIsTrue:
                        back = webWaitUntilFunc.WebWaitUntilIsTrue(sigteststep);
                        break;
                    case RoWebStep.WebWaitUntilStringContains:
                        back = webWaitUntilFunc.WebWaitUntilStringContains(sigteststep);
                        break;
                    case RoWebStep.WebWaitUntilStringLength:
                        back = webWaitUntilFunc.WebWaitUntilStringLength(sigteststep);
                        break;
                }


                //通过back中的信息进行outconsole输出

                //开始判断是否错误转为警告
                if (ew)
                {
                    if (back.Result)
                    {
                        if (RoPubArgments.ProTips)
                        {
                            RoOutputToGui roOutputToGui = new RoOutputToGui();
                            roOutputToGui.OutResult(Path.GetFileNameWithoutExtension(RoPubArgments.GuiTestArgsInfo.TcsFilePath), sigteststep.Name, back.ControlId, "Pass", back.ExtraInfo);
                        }
                        else
                        {
                            roOutputToConsole.OutputConsole(RoLogStatus.Pass, sigteststep.Name, back.ControlId, back.ExtraInfo);
                        }
                    }
                    else
                    {
                        if (RoPubArgments.ProTips)
                        {
                            RoOutputToGui roOutputToGui = new RoOutputToGui();
                            roOutputToGui.OutResult(Path.GetFileNameWithoutExtension(RoPubArgments.GuiTestArgsInfo.TcsFilePath), sigteststep.Name, back.ControlId, "Waring", back.ExtraInfo);
                        }
                        else
                        {
                            roOutputToConsole.OutputConsole(RoLogStatus.Warning, sigteststep.Name, back.ControlId, back.ExtraInfo);
                        }
                    }

                    BackStepResult = true;
                }
                //如果不转为警告
                else
                {
                    if (back.Result)
                    {
                        if (RoPubArgments.ProTips)
                        {
                            RoOutputToGui roOutputToGui = new RoOutputToGui();
                            roOutputToGui.OutResult(Path.GetFileNameWithoutExtension(RoPubArgments.GuiTestArgsInfo.TcsFilePath), sigteststep.Name, back.ControlId, "Pass", back.ExtraInfo);
                        }
                        else
                        {
                            roOutputToConsole.OutputConsole(RoLogStatus.Pass, sigteststep.Name, back.ControlId, back.ExtraInfo);
                        }
                        BackStepResult = true;
                    }
                    else
                    {
                        if (RoPubArgments.ProTips)
                        {
                            RoOutputToGui roOutputToGui = new RoOutputToGui();
                            roOutputToGui.OutResult(Path.GetFileNameWithoutExtension(RoPubArgments.GuiTestArgsInfo.TcsFilePath), sigteststep.Name, back.ControlId, "Fail", back.ExtraInfo);
                        }
                        else
                        {
                            roOutputToConsole.OutputConsole(RoLogStatus.Fail, sigteststep.Name, back.ControlId, back.ExtraInfo);
                        }
                        BackStepResult = false;
                    }
                }
            }
            catch (Exception e)
            {
                ExceptionOutPut.SystemExceptionOut("RunStepsQuene", e);
                //步骤执行失败或异常
                BackStepResult = false;
            }
        }
    }
}
